#! /bin/sh
# force
# filename filepath filemd5

filename=filelist.txt
#对IFS变量 进行替换处理
OLD_IFS="$IFS"
IFS=" "
nameindex=0
pathindex=1
md5index=2
folderindex=3
index=0
name=""
filepath=""
md5value=""
folder=""
maybe_doload()
{
        fullpath="$4/$1"
        echo "fullpath"$fullpath
        if test ! -d $4;then
                mkdir -p $4
        fi
        if test -e $fullpath;then
                filemd5sum=$(md5sum $fullpath | cut -d ' ' -f 1)
                if [ "$filemd5sum" = $3 ];then
                        echo "file "$fullpath" is already ready"
                        return
                fi
                mv $fullpath $fullpath".bak"
        fi
        wget $2
        mv $1 $fullpath
}
cat $filename | while read line
do
        index=0
        name=""
        filepath=""
        md5value=""
        for var in $line
        do
                if [ $index -eq $nameindex ];then
                        name=$var
                elif [ $index -eq $pathindex ];then
                        filepath=$var
                elif [ $index -eq $md5index ];then
                        md5value=$var
                else
                        folder=$var
                fi
                index=`expr $index + 1`
        done
        if [ $name ]  && [ $filepath ]  && [ $md5value ];then
                if test -z $folder;then
                        $folder="."
                fi
                echo "stat check name:"$name" path:"$filepath" md5:"$md5value" folder:"$folder
                maybe_doload $name $filepath $md5value $folder
        else
                echo "not valid cfg line" $line
        fi
done

IFS="$OLD_IFS"
